package cate.game.role.task.msg;

import cate.common.annotation.OperationSetter;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDOperation;
import cate.common.util.GameResult;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBTask;
import cate.game.role.task.TaskItem;
import easy.java.dev.note.NoteField;

import java.util.List;
import java.util.Objects;

@OperationSetter(value = GDOperation.TASK_RWD, sub = "funcId")
@ClientMsg(title = "根据功能ID领取所有奖励 の 请求")
public class TaskGetMutiRwdReq implements MsgInput {
    @NoteField(value = "任务组所属功能ID")
    public int funcId;

    @Override
    public MsgConfig conf() {
        return MsgIdBTask.GET_MUTI_RWD_REQ;
    }

    @Override
    public void execute(GameBody game, ClientContext cc) {
        GameResult<List<TaskItem>> r = cc.getRole().getTask().getMultiRwd(funcId);
        if (!r.ok()) {
            cc.getRole().getGame().notice.message(cc.getRole(), r.fail());
            return;
        }

        List<TaskItem> tasks = r.data;
        if (Objects.nonNull(tasks) && !tasks.isEmpty()) {
            cc.getRole().sendNow(new TaskGroupUpdateResp(funcId, tasks));
            if (funcId == GDFunc.MAINLINE) {
                // 如果是主线任务，那么删除该任务，接下来加入下一个任务
                cc.getRole().getTask().mainline.addNextTask();
            } else if (funcId == GDFunc.ACHIEVEMENT) {
                cc.getRole().getHistory().action.receiveAchievementReward();
            } else if (funcId == GDFunc.TASK_WEEKLY) {
                cc.getRole().getHistory().action.weeklyTaskRewardGet(tasks.size());
            } else if (funcId == GDFunc.TASK_DAILY) {
                cc.getRole().getHistory().action.dailyTaskRewardGet(tasks.size());
            }
        }
    }
}
